﻿#include "ui_mainwindow.h"
#include "image_push_button.h"
#include "ssnavbutton.h"
#include "round_widget.h"
#include "shadow_widget.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedWidget>
#include <QLabel>
#include <QApplication>
#include <QToolButton>
#include <QButtonGroup>

Ui_MainWindow::Ui_MainWindow(QObject *parent)
    : QObject(parent)
{
}

/**
 * @brief
 * @param parent
 */

void Ui_MainWindow::setupUi(ShadowWidget *widget)
{
    if (NULL == widget)
    {
        return;
    }

    widget->setShadowWidth(10);
    m_layout_root = widget->getRootLayout();

    RoundWidget *innerFrame = new RoundWidget;
    innerFrame->setRound(5, 5);
    m_layout_root->addWidget(innerFrame);

    QVBoxLayout *layout_main = new QVBoxLayout;
    layout_main->setContentsMargins(0, 0, 0, 0);
    layout_main->setSpacing(0);
    innerFrame->setLayout(layout_main);

    // topbar开始
    QWidget *topbar_widget = new QWidget; // 用于设置topbar背景色
    layout_main->addWidget(topbar_widget);
    topbar_widget->setFixedHeight(40);
    topbar_widget->setObjectName(QStringLiteral("topbar"));

    QHBoxLayout *layout_topbar_widget = new QHBoxLayout;
    layout_topbar_widget->setContentsMargins(0, 0, 0, 0);

    QWidget *bar_left = new QWidget;
    QWidget *bar_middle = new QWidget;
    QWidget *bar_right = new QWidget;
    layout_topbar_widget->addWidget(bar_left);
    layout_topbar_widget->addWidget(bar_middle);
    layout_topbar_widget->addWidget(bar_right);
    layout_topbar_widget->setStretch(0, 5);
    layout_topbar_widget->setStretch(1, 10);
    layout_topbar_widget->setStretch(2, 5);

    topbar_widget->setLayout(layout_topbar_widget);

    // 开始创建bar_left内容.
    QVBoxLayout *layout_bar_left = new QVBoxLayout;
    bar_left->setLayout(layout_bar_left);

    // 放置程序图标和标题 -----------------------------------
    QHBoxLayout *layout_title = new QHBoxLayout;
    layout_bar_left->addLayout(layout_title);
    layout_title->setContentsMargins(0, 0, 0, 0);

    QLabel *app_icon = new QLabel;
    // 在这里添加载入程序图标的代码
    // To do...
    app_icon->setObjectName(QStringLiteral("app-icon"));
    layout_title->addWidget(app_icon);

    QLabel *app_title = new QLabel;
    app_title->setObjectName(QStringLiteral("app-title"));
    app_title->setText(QObject::tr("app_title"));

    layout_title->addWidget(app_title);
    layout_title->setStretch(0, 1);
    layout_title->setStretch(1, 6);

    // 开始创建bar_middle内容：界面切换按钮组.
    QHBoxLayout *layout_bar_middle = new QHBoxLayout;
    bar_middle->setLayout(layout_bar_middle);
    layout_bar_middle->setContentsMargins(12, 0, 12, 0);
    layout_bar_middle->setSpacing(0);

    // 弹簧
    layout_bar_middle->addStretch();

    // 开始创建bar_right内容.
    QVBoxLayout *layout_bar_right = new QVBoxLayout;
    bar_right->setLayout(layout_bar_right);
    layout_bar_right->setContentsMargins(0, 0, 5, 0);

    // 系统按钮
    QHBoxLayout *sysbtns = new QHBoxLayout;
    layout_bar_right->addLayout(sysbtns);
    sysbtns->setDirection(QBoxLayout::RightToLeft);
    sysbtns->setSpacing(0);

    m_sysbtn_close = new ImagePushButton;
    m_sysbtn_close->setImageName(QStringLiteral(":/img/sysButton/close"));
    m_sysbtn_close->setToolTip(QObject::tr("close"));

    m_sysbtn_max = new ImagePushButton;
    m_sysbtn_max->setImageName(QStringLiteral(":/img/sysButton/max"));
    m_sysbtn_max->setToolTip(QObject::tr("maximize"));

    m_sysbtn_restore = new ImagePushButton;
    m_sysbtn_restore->setImageName(QStringLiteral(":/img/sysButton/restore"));
    m_sysbtn_restore->setToolTip(QObject::tr("restore"));
    m_sysbtn_restore->hide();

    m_sysbtn_min = new ImagePushButton;
    m_sysbtn_min->setImageName(QStringLiteral(":/img/sysButton/min"));
    m_sysbtn_min->setToolTip(QObject::tr("minimize"));

    m_sysbtn_menu = new ImagePushButton;
    m_sysbtn_menu->setObjectName(QObject::tr("menu"));

    sysbtns->addWidget(m_sysbtn_close);
    sysbtns->addWidget(m_sysbtn_max);
    sysbtns->addWidget(m_sysbtn_restore);
    sysbtns->addWidget(m_sysbtn_min);
    sysbtns->addWidget(m_sysbtn_menu);
    sysbtns->addStretch();

    //layout_bar_right->addStretch();

    // 中间部分：导航 + 内容页.
    QHBoxLayout *layout_middle_bar = new QHBoxLayout;
    layout_main->addLayout(layout_middle_bar);
    layout_middle_bar->setContentsMargins(0, 0, 0, 0);

    // 导航条
    QWidget *navBar = new QWidget;
    layout_middle_bar->addWidget(navBar);
    navBar->setObjectName("navBar");

    QVBoxLayout *layout_navBar = new QVBoxLayout;
    navBar->setLayout(layout_navBar);
    layout_navBar->setContentsMargins(0, 0, 0, 0);
    layout_navBar->setSpacing(0);

    // 互斥按钮组
    m_navBar_buttons = new QButtonGroup(this);

    // nav1
    NavButton *nav1 = new NavButton;
    nav1->setText(tr("Nav1"));
    layout_navBar->addWidget(nav1);
    m_navBar_buttons->addButton(nav1, PAGE_NAV1);
    nav1->setChecked(true);

    // nav2
    NavButton *nav2 = new NavButton;
    layout_navBar->addWidget(nav2);
    nav2->setText(tr("Nav2"));
    m_navBar_buttons->addButton(nav2, PAGE_NAV2);

    // nav3
    NavButton *nav3 = new NavButton;
    layout_navBar->addWidget(nav3);
    nav3->setText(tr("Nav3"));
    m_navBar_buttons->addButton(nav3, PAGE_NAV3);

    layout_navBar->addStretch();

    // 内容页容器
    // 方案一：按照顺序添加，需要注意顺序。
    // 方案二：使用insertWidget配合index添加，不容易出错。
    QWidget *centre_widget = new QWidget;
    layout_middle_bar->addWidget(centre_widget);
    centre_widget->setObjectName(QStringLiteral("centre_widget"));

    QVBoxLayout *layout_centre = new QVBoxLayout;
    centre_widget->setLayout(layout_centre);
    layout_centre->setContentsMargins(10, 10, 10, 10);

    m_page_container = new QStackedWidget;
    layout_centre->addWidget(m_page_container);

    // 内容页
    // PAGE1
    QWidget *page_nav1 = new QWidget;
    QVBoxLayout *vLay = new QVBoxLayout;
    page_nav1->setLayout(vLay);

    QLabel *label_test = new QLabel(QStringLiteral("PAGE1"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#9933FF");

    m_page_container->insertWidget(PAGE_NAV1, page_nav1);

    // PAGE2
    QWidget *page_nav2 = new QWidget;
    vLay = new QVBoxLayout;
    page_nav2->setLayout(vLay);

    label_test = new QLabel(QStringLiteral("PAGE2"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#00CC00;");

    m_page_container->insertWidget(PAGE_NAV2, page_nav2);

    // PAGE3
    QWidget *page_nav3 = new QWidget;
    vLay = new QVBoxLayout;
    page_nav3->setLayout(vLay);

    label_test = new QLabel(QStringLiteral("PAGE3"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#CC3333;");

    m_page_container->insertWidget(PAGE_NAV3, page_nav3);

    // 底部(状态)栏
    QWidget *bottom_bar_widget = new QWidget;
    bottom_bar_widget->setFixedHeight(30);
    bottom_bar_widget->setObjectName("bottom_bar");
    layout_main->addWidget(bottom_bar_widget);

    // 设置布局比例
    //layout_main->setStretch(0, 10);
    //layout_main->setStretch(1, 150);
    //layout_main->setStretch(2, 10);

    //
    createConnections();
}

void Ui_MainWindow::createConnections()
{
    connect(m_sysbtn_close, SIGNAL(clicked(bool)), this, SLOT(onCloseBtnClicked()));
    connect(m_sysbtn_max, SIGNAL(clicked(bool)), this, SLOT(onMaxBtnClicked()));
    connect(m_sysbtn_restore, SIGNAL(clicked(bool)), this, SLOT(onRestoreBtnClicked()));
    connect(m_sysbtn_min, SIGNAL(clicked(bool)), this, SLOT(onMinBtnClicked()));
}

void Ui_MainWindow::onMinBtnClicked()
{
    showMinimized();
}

void Ui_MainWindow::onMaxBtnClicked()
{
   m_sysbtn_max->hide();
   m_sysbtn_restore->show();
   showMaximized();
}

void Ui_MainWindow::onRestoreBtnClicked()
{
   m_sysbtn_max->show();
   m_sysbtn_restore->hide();
   showNormal();
}

void Ui_MainWindow::onCloseBtnClicked()
{
    close();
}







